package com.cap.dreamcircle.View;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Base.BaseActivity;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.Bean.DreamLabelDetailBean;
import com.cap.dreamcircle.Model.Bean.DreamLabelDetailEntity;
import com.cap.dreamcircle.Model.IDreamModel;
import com.cap.dreamcircle.Model.Impl.DreamModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.TransformUtils;
import com.cap.dreamcircle.View.Adapter.DreamLabelDetailAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action0;

/**
 * Created by liucxu on 2017/8/24.
 */

public class DreamLabelStreamActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.layout_back)
    LinearLayout layout_back;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipe_refresh_layout;
    @BindView(R.id.list_view)
    RecyclerView list_view;

    private List<DreamLabelDetailEntity> datas = new ArrayList<>();
    private DreamLabelDetailAdapter adapter;
    private IDreamModel dreamModel;

    private String tagId;
    private String tagName;

    @Override
    public int getLayoutId() {
        return R.layout.activity_dream_label_stream;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this);
        dreamModel=new DreamModelImpl();
        list_view.setLayoutManager(new LinearLayoutManager(this));
        adapter=new DreamLabelDetailAdapter(this,datas);
        list_view.setAdapter(adapter);
        layout_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.GetInstance().finishActivity(DreamLabelStreamActivity.this);
            }
        });
        swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doRequestDreamLabelStream();
            }
        });
    }

    @Override
    public void setupData() {
        Intent intent=getIntent();
        tagId=intent.getStringExtra("tagId");
        tagName=intent.getStringExtra("tagName");
        if(TextUtils.isEmpty(tagId)||TextUtils.isEmpty(tagName)){
            App.GetInstance().finishActivity(DreamLabelStreamActivity.this);
        }
        else{
            tv_title.setText(tagName);
            doRequestDreamLabelStream();
        }
    }

    private void doRequestDreamLabelStream(){
        dreamModel.RequestDreamLabelDetail(tagId,tagName).compose(TransformUtils.<DreamLabelDetailBean>defaultSchedulers()).doOnSubscribe(new Action0() {
            @Override
            public void call() {
//                showProgress();
            }
        }).subscribe(new CommonSubscriber<DreamLabelDetailBean>() {
            @Override
            public void onSuccess(DreamLabelDetailBean response) {
//                hideProgress();
                swipe_refresh_layout.setRefreshing(false);
                adapter.setDatas(response.getDreamResult());
            }

            @Override
            public void onFailure(BaseBean response) {
//                hideProgress();
                swipe_refresh_layout.setRefreshing(false);
            }
        });
    }
}
